<?php

declare(strict_types=1);

namespace app\agent\controller\order;

use app\agent\controller\Controller;
use app\model\OilAgentConfig;
use app\model\OilOrder as Model;
use think\facade\Db;
use think\Request;

/**
 * @apiDefine oilAgent 代理商后台
 */
class Oil extends Controller
{
    /**
     * @api {GET} /agent/order.oil/ 订单列表
     * @apiName 代理商订单列表
     * @apiDescription <font color="red"><B>note:字段意义可以参考总管理后台-订单列表<B></font>
     * @apiGroup oilAgent
     * @apiParam {Number} [cnid] 渠道
     * @apiParam {Number=0,1} [refund_status] 订单状态 1退款 0正常 不传查询全部
     * @apiParam {String} [out_order_id] 订单号
     * @apiParam {String} [pay_time[start]] 支付时间  开始
     * @apiParam {String} [pay_time[end]] 支付时间 结束
     * @apiSuccessExample {json} 成功返回
     * {
     * 	"code": 200,
     * 	"message": "",
     * 	"data": {
     * 		"total": 1,
     * 		"per_page": 15,
     * 		"current_page": 1,
     * 		"last_page": 1,
     * 		"data": [{
     * 			"id": 1,
     * 			"user_id": 14,
     * 			"out_order_id": "1111",
     * 			"total_amount": 100,
     * 			"pay_amount": 100,
     * 			"pay_type": 1,
     * 			"status": "已完成",
     * 			"supplier_id": 1,
     * 			"agent_id": 1,
     * 			"cnid": 1,
     * 			"pay_time": "2021-09-18 09:57:02",
     * 			"store_id": "5021354570330676040",
     * 			"store_name": "",
     * 			"gun_no": "",
     * 			"item_name": 0,
     * 			"city_name": "",
     * 			"quantity": "0.00",
     * 			"settle_status": 0,
     * 			"refund_status": 0,
     * 			"refund_at": null,
     * 			"channel_name": "渠道1",
     * 			"agent_name": "代理商",
     * 			"commission": 0
     * 		}]
     * 	}
     * }
     * @apiUse PageReturn
     * @apiVersion 1.0.0
     */
    public function index(Request $request)
    {
        $commissionRate = 0;
        if ($this->agent['oil_service']) { // 开启加油服务
            $agentConfig = OilAgentConfig::where('agent_id', $this->agent['id'])->find();
            if ($agentConfig) {
                $commissionRate = $agentConfig['commission_rate'];
            }
        }

        $where = [['oil_order.status', '=', 1], ['oil_order.agent_id', '=', $this->agent['id']]];
        if ($request->has('refund_status')) {
            $where[] = ['refund_status', '=', $request->param('refund_status')];
        }

        $param = $request->only(['cnid' => 0, 'out_order_id' => '', 'pay_time' => [], 'refund_time' => []]);
        if ($param['cnid']) {
            $where[] = ['cnid', '=', $param['cnid']];
        }
        if ($param['out_order_id']) {
            $where[] = ['out_order_id', 'like', '%'.$param['out_order_id'].'%'];
        }
        if ($param['pay_time'] && is_array($param['pay_time'])) {
            if (!empty($param['pay_time']['start'])) {
                $where[] = ['pay_time', '>=', $param['pay_time']['start']];
            }
            if (!empty($param['pay_time']['end'])) {
                $where[] = ['pay_time', '<=', $param['pay_time']['end']];
            }
        }

        /* if ($param['refund_time'] && is_array($param['refund_time'])) {
            $where[] = ['refund_status', '=', 1];
            if (!empty($param['refund_time']['start'])) {
                $where[] = ['refund_at', '>=', $param['refund_time']['start']];
            }
            if (!empty($param['refund_time']['end'])) {
                $where[] = ['refund_at', '<=', $param['refund_time']['end']];
            }
        } */

        $model = new Model();
        $list = $model->adminList($where, $commissionRate, $request->only(['page' => 1, 'list_rows' => 15]));

        return success('操作成功', $list);
    }

    /**
     * 渠道列表.
     */
    public function channelList()
    {
        $list = Db::name('oil_agent_channel')
            ->field('id, name')
            ->where('agent_id', $this->agent['id'])
            ->where('status', 1)
            ->select()
            ->toArray();

        return success('操作成功', $list);
    }
}
